<?php

namespace App\Http\Controllers\Wechat;

use App\Models\Category;
use App\Models\CategoryUser;
use App\Models\Goods;
use App\Models\GoodsCustom;
use App\Models\GoodsSetmeal;
use App\Models\GoodsSpec;
use App\Services\CommonService;
use App\Services\GoodsService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;

class GoodsController
{
    /**
     * 商品详情
     */
    public function getGoodsInfo(Request $request){
        $goods_id = $request->input('goods_id');
        if($goods_id){
            // $goods = Redis::hget(GoodsService::GOODS_INFO_KEY,$goods_id);
            // if($goods){
            //     $goods = json_decode($goods,true);
            // }else{
                $goods = Goods::getGoodsInfo($goods_id);
                if(!empty($goods)){
                    $goods['month_rent'] = ceil($goods['min_price'] * 30);
                    $goods['max_foregift'] = GoodsSetmeal::getMaxForegift($goods_id);
                    $shop_category = CategoryUser::GetShopCategory($goods['member_id']);
                    $goods['category'] = implode('、',array_column($shop_category,'name'));
                    $goods['convey_type'] = '寄出'.GoodsService::GOODS_SEND_CONVEY_TYPE_NAME_LIST[$goods['convey_type1']].'/归还'.GoodsService::GOODS_SEND_CONVEY_TYPE_NAME_LIST[$goods['convey_type2']];
                    unset($goods['convey_type1']);
                    unset($goods['convey_type2']);
                    Redis::hset(GoodsService::GOODS_INFO_KEY,$goods_id,json_encode($goods));
                }else{
                    return ReturnMsg(CommonService::HTTP_CODE_FAIL, '商品已下架');
                }
            // }
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$goods);
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '缺少商品ID');
        }
    }


    /**
     * 获取商品套餐信息
     */
    public function getGoodsSetmeal(Request $request){
        $goods_id = $request->input('goods_id');
        if($goods_id){
            $setmeal = GoodsSetmeal::getGoodsSetmeal($goods_id);
            foreach ($setmeal as $k=>$v){
                $setmeal[$k]['title'] = $v['title'].'('.GoodsService::GOODS_RENT_TYPE_NAME_LIST[$v['lease_set']].GoodsService::GOODS_BUYOUT_NAME_LIST[$v['is_buy_out']].')';
                $setmeal[$k]['spec'] = GoodsSpec::GetGoodsSpecData($v['goods_id'],$v['id'],$v['lease_set'],$v['is_huabei']);
            }
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$setmeal);
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '缺少商品ID');
        }
    }


    /**
     * 获取商品参数信息
     */
    public function getGoodsParameter(Request $request){
        $goods_id = $request->input('goods_id');
        if($goods_id){
            $data = GoodsCustom::getGoodsAttribute($goods_id);
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$data);
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '缺少商品ID');
        }
    }

    /**
     * 获取分类
     */
    public function getCategory(){
        $data = Redis::get(Category::REDIS_CATEGORY);
        if(empty($data)){
            $category = Category::where([['enabled',Category::CATEGORY_STATUS_DISPLAY],['is_delete',Category::CATEGORY_NOT_DELETE]])
                ->select('id','name','image','parentid')->orderBy('displayorder','asc')->get();
            $category = $category ? $category->toArray() : [];
            $data = [];
            if(!empty($category)){
                $generateTree = function ($items, $parentKey = 'parentid') {
                    $tree = $temp = [];
                    foreach ($items as $item) {
                        $temp[$item['id']] = $item;
                    }
                    foreach ($items as $item) {
                        if (isset($temp[$item[$parentKey]])) {
                            $temp[$item[$parentKey]]['sub'][] = &$temp[$item['id']];
                        } else {
                            $tree[] = &$temp[$item['id']];
                        }
                        // 下面可以去掉 pid 元素
                        unset($temp[$item['id']][$parentKey]);
                    }
                    return $tree;
                };
                $data = $generateTree($category);
                Redis::set(Category::REDIS_CATEGORY,json_encode($data));
            }
        }else{
            $data = json_decode($data,true);
        }
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$data);
    }

    /**
     * 获取商品列表
     */
    public function getGoodsList(Request $request){
        $cate_id = $request->input('cate_id');
        $goods_name = trim($request->input('goods_name'));
        $orderby_name = $request->input('orderby_name','id');
        $sort = $request->input('sort','desc');
        $convey_type = $request->input('convey_type','');
        $min_price = $request->input('min_price','');
        $max_price = $request->input('max_price','');
        $page = $request->input('page');
        $pageSize = $request->input('pageSize')??50;
        $where[] = ['status',GoodsService::GOODS_STATUS_PUT];
        $where[] = ['is_show',GoodsService::GOODS_IS_SHOW_YES];
        $where[] = ['is_delete',CommonService::GOODS_IS_DELETE_NOT];
        $where[] = ['member_id','<>','1693377836'];
        if(!empty($cate_id)){
            $where[] = ['category_id',$cate_id];
        }
        if(!empty($goods_name)){
            $where[] = ['title','like','%'.trim($goods_name).'%'];
        }
        if(!empty($convey_type)){
            $where[] = ['convey_type1',$convey_type];
        }
        if(!empty($min_price)){
            $where[] = ['min_price','>',$min_price];
        }
        if(!empty($max_price)){
            $where[] = ['min_price','<',$max_price];
        }
        $goods = Goods::where($where)->select('id','title','rent_label','imgurl','min_price','min_lease_term','is_huabei')->orderby($orderby_name,$sort)->paginate($pageSize);
        $goods = $goods ? $goods->toArray() : [];
        if(!empty($goods['data'])){
            foreach ($goods['data'] as $k=>$v){
                if(GoodsSetmeal::where([['goods_id',$v['id']],['is_delete',CommonService::GOODS_IS_DELETE_NOT],['is_huabei',GoodsService::GOODS_SETMEAL_IS_HUABEI]])->count() > 0){
                    $goods['data'][$k]['is_huabei'] = GoodsService::GOODS_SETMEAL_IS_HUABEI;
                }
            }
        }
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $goods);
    }

    /**
     * 获取商品碎屏险
     */
    public function getInsuranceList(Request $request){

        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', GoodsService::GOODS_INSURANCE_LIST);

    }

    /**
     * 获取联通冲浪卡
     * @param Request $request
     * @return false|string
     */
    public function getCardList(Request $request){
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', GoodsService::GOODS_UNIWAP_LIST);
    }



}
